package com.app.changebath;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class ChangeBath extends Activity {
	private EditText text;

	/** Called when the activity is first created. */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		text = (EditText) findViewById(R.id.EditText01);
	}

	public void myClickHandler(View view) {
		switch (view.getId()) {
		case R.id.Button01:
			RadioButton milesButton = (RadioButton) 
					findViewById(R.id.RadioButton01);
			RadioButton kilometersButton = (RadioButton) 
					findViewById(R.id.RadioButton02);
			if (text.getText().length() == 0) {
				Toast.makeText(this, 
						"Invalid Number", 
						Toast.LENGTH_LONG).show();
				return;
			}

			float inputValue = Float.parseFloat
					(text.getText().toString());
			if (milesButton.isChecked()) {
				text.setText(String.valueOf
						(convertToKilometers(inputValue)));
			} else {
				text.setText(String.valueOf
						(convertToMiles(inputValue)));
			}
			// Switch to the other button
			if (milesButton.isChecked()) {
				milesButton.setChecked(false);
				kilometersButton.setChecked(true);
			} else {
				milesButton.setChecked(true);
				kilometersButton.setChecked(false);
			}
			break;
		}
	}

	private float convertToKilometers(float miles) {
		return (float) (miles * 1.609344);
	}

	private float convertToMiles(float kilometers) {
		return (float) (kilometers * 0.621371192);
	}
}
